﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using AVIS.OutBound.FrequentTravellerPartners.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.FrequentTravellerPartners
{
    public class FrequentTravellerPartnerManger : DomainService, IFrequentTravellerPartnerManger
    {
        #region 构造函数
        private readonly ICacheManager _cacheManager;
        private readonly IObjectMapper _objectMapper;
        private readonly IRepository<FrequentTravellerPartner, string> _ftRepository;
        public FrequentTravellerPartnerManger(ICacheManager cacheManager,
            IObjectMapper objectMapper,
            IRepository<FrequentTravellerPartner, string> ftRepository
            )
        {
            _objectMapper = objectMapper;
            _cacheManager = cacheManager;
            _ftRepository = ftRepository;
        }
        #endregion

        public List<FrequentTravellerpartnerCacheItem> AllFrequentTravellerpartner()
        {
            var cacheItem = _cacheManager.GetFrequentTravellerpartnerCache().Get(FrequentTravellerpartnerCacheItem.CacheName, f =>
            {
                var query = from t in _ftRepository.GetAll()
                            orderby t.SeqNo, t.Name
                            select t;

                return _objectMapper.Map<List<FrequentTravellerpartnerCacheItem>>(query.ToList());
            });
            return cacheItem;
        }
    }
}
